// 遍历获取试题数据
import moment from 'moment';
import $ from 'jquery';
import { getCookie } from './cookies';
export function getQuestionCount(
  questionList,
  modeType,
  total = 0,
  doCount = 0,
  rightCount = 0,
  sectionTotal = 0,
  wrongCount = 0,
  subjectiveCount = 0,
) {
  //hasSubjective 主观题

  const questions = questionList;
  if (questions.length > 0) {
    for (let j in questions) {
      total++;
      if (
        questions[j]._did ||
        (questions[j].MyAnswer &&
          (modeType === 'analysis' ||
            modeType === 'exam' ||
            modeType === 'chaptexer') &&
          questions[j].MyAnswer)
      ) {
        //已做标志
        doCount++;
        if (questions[j].QuestionType_ID === 5) {
          subjectiveCount++;
        } else if (questions[j].Judge === 0) {
          //根据题型判断正误
          if (
            questions[j].QuestionType_ID === 1 ||
            questions[j].QuestionType_ID === 4 ||
            modeType !== 'chaptexer' || questions[j]._submit
          ) {
            wrongCount++;
          }
        }
      }
      if (questions[j].Judge === 1) {
        //根据题型判断正误
        if (
          questions[j].QuestionType_ID === 1 ||
          questions[j].QuestionType_ID === 4 ||
          modeType !== 'chaptexer' || questions[j]._submit
        ) {
          rightCount++;
        }
      }
    }
  }

  return {
    sectionTotal,
    total,
    doCount,
    rightCount,
    wrongCount,
    subjectiveCount,
  };
}

//更新习题配置信息
export function updateExerConfig({ ...params }) {
  let exerConfig = localStorage.getItem('exerConfig');
  exerConfig = exerConfig ? JSON.parse(exerConfig) : {};
  exerConfig = { ...exerConfig, ...params };
  localStorage.setItem('exerConfig', JSON.stringify(exerConfig));
}

export function getExerConfig() {
  let exerConfig = localStorage.getItem('exerConfig');
  return (exerConfig && JSON.parse(exerConfig)) || {};
}

export function formatNumCN(num) {
  //题目序号
  switch (num) {
    case 1:
      return '一';
    case 2:
      return '二';
    case 3:
      return '三';
    case 4:
      return '四';
    case 5:
      return '五';
    case 6:
      return '六';
    case 7:
      return '七';
    case 8:
      return '八';
    case 9:
      return '九';
    case 10:
      return '十';
  }
}
export function QuestionTypeName(num) {
  //题目序号
  switch (num) {
    case 1:
      return '单选题';

    case 2:
      return '多选题';

    case 3:
      return '填空题';

    case 4:
      return '判断题';

    case 5:
      return '问答题';

    case 6:
      return '计算题';

    case 7:
      return '组合题';

    default:
      return '未知题型';
  }
}
export const SortByNoteTime = (a, b) => {
  return moment(b.AddTime) - moment(a.AddTime);
};
export function isLogin() {
  return getCookie('UserKey') != 'undefined' && getCookie('UserKey');
}
export const addTime = ({ timeText = '00:00:00', times = 0 }, t = 1) => {
  let { h, m, s } = SecTimeObj(times);
  s = s + t;
  if (s === 60) {
    s = 0;
    m = m + 1;
    if (m === 60) {
      m = 0;
      h = h + 1;
    }
  }
  times = s + m * 60 + h * 3600;
  if (s < 10) {
    s = '0' + s;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (h < 10) {
    h = '0' + h;
  }
  return { text: h + ':' + m + ':' + s, times };
};

export const reduceTime = ({ timeText = '00:90:00', times = 5400 }, t = 1) => {
  let { h, m, s } = SecTimeObj(times);
  s = s - t;
  if (s < 0) {
    s = 59;
    m = m - 1;
    if (m < 0) {
      m = 59;
      h = h - 1;
    }
    if (h < 0) {
      h = 0;
    }
  }
  times = s + m * 60 + h * 3600;
  if (s < 10) {
    s = '0' + s;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (h < 10) {
    h = '0' + h;
  }
  return { text: h + ':' + m + ':' + s, times };
};
export function SecTimeObj(time) {
  let s = 0,
    m = 0,
    h = 0;
  s = time % 60 || 0;
  m = parseInt(((time % 3600) - s) / 60) || 0;
  h = parseInt(time / 3600) || 0;
  return { h, m, s };
}

export function RouterGoBack(params, myHistory) {
  // 路由记录缓存 跳转；默认返回上层
  const { index = 1, defaultUrl, type = 'replace' } = params || {};
  let RouterArray =
    JSON.parse(localStorage.getItem('RouterArray') || '[]') || [];
  let oraUrl = RouterArray[0];
  if (RouterArray.length > index) {
    if (index === 1) {
      let url = RouterArray[index];
      if (url.match('/myexer/') === '/myexer/') {
      } else if (url.match('/testSet/') === '/testSet/') {
        //跳过设置页面
        url = '/myexer';
      } else if (oraUrl.match('/analysis/') === '/analysis/') {
        //解析页面 多一层判断
        if (RouterArray.length > index + 2) {
          let url2 = RouterArray[index + 1];
          if (url2.match('/testSet/') === '/testSet/') {
            url = '/myexer';
          } else {
            url = '/testrecord/exer';
          }
        } else {
          url = '/testrecord/exer';
        }
      } else {
        //其他全跳记录题
        url = '/testrecord/exer';
      }
      if (type === 'replace') {
        myHistory.replace(url);
      } else {
        myHistory.push(url);
      }
    }
  } else if (defaultUrl) {
    if (type === 'replace') {
      myHistory.replace(defaultUrl);
    } else {
      myHistory.push(defaultUrl);
    }
  } else {
    console.error('RouterGo error');
  }
}

export const PopClicked = e => {
  //泡泡弹框 漂移问题
  if (BrowserInfo().type === 'IE') {
    const { left, top } = $(e.target).offset();
    window.poptimer = setInterval(() => {
      var pop = $('.ant-popover');
      if (pop.length > 0 && pop[0].style) {
        //存在立即中断
        for (let i in pop) {
          if (parseInt(i) >= 0) {
            pop[i].style.left = '0px';
            pop[i].style.marginLeft = left - 50 + 'px';
          }
        }
        return window.poptimer && clearInterval(window.poptimer);
      }
      pop = $('.ant-popover');
      // pop = document.getElementsByClassName('ant-popover')//ie10不支持？？
    }, 0);
  }
};

export const BrowserInfo = (type, version) => {
  // 判断ie浏览器版本
  if (!!window.ActiveXObject || 'ActiveXObject' in window) {
    //ie
    type = 'IE';
    if (
      navigator.appName === 'Microsoft Internet Explorer' &&
      navigator.appVersion.match(/9./i) === '9.'
    ) {
      version = '9';
    } else if (
      navigator.appName === 'Microsoft Internet Explorer' &&
      navigator.appVersion.match(/10./i) === '10.'
    ) {
      version = '10';
    } else if (
      navigator.appName === 'Netscape' &&
      navigator.appVersion.match(/rv:11./i) === 'rv:11.'
    ) {
      version = '11';
    } else if (
      navigator.appName === 'Netscape' &&
      navigator.appVersion.match(/^4.0/i) === '4.0'
    ) {
      //ie11兼容性视图
      version = '11';
    } else if (
      navigator.appName === 'Netscape' &&
      navigator.appVersion.match(/^5.0/i) === '5.0'
    ) {
      //ie11标准视图
      version = '11';
    }
  } else {
    type = 'other';
    version = 'other';
  }
  return { version, type };
};
